﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using JM.JCY.BusinessEntity;
using JM.JCY.Proxy;
using NCS.IConnect.ApplicationContexts;
using JM.JCY.Common;
using JM.JCY.Common.Utilities;
using Microsoft.SharePoint;


namespace JM.JCY.SharePointComponent.PerformanceArchives.PerformanceCaseDetails
{
    public partial class PerformanceCaseDetailsUserControl : UserControl
    {
        public int _caseId
        {
            set { ViewState["_caseId"] = value; }
            get { return (int)ViewState["_caseId"]; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (PerformanceCaseProxy proxy = new PerformanceCaseProxy())
                {
                    if (Request.QueryString["CaseId"] != null)
                    {
                        _caseId = Int32.Parse(Request.QueryString["CaseId"].ToString());
                    }
                    // load data
                    var loadDT = proxy.GetPerformanceCase(_caseId, string.Empty);
                    LoadForEdit(loadDT);
                    LoadForView(loadDT);
                    Session["EditPerformanceCaseDT"] = loadDT;
                }
            }

            // switch controls
            if (this.hfIsEdit.Value != "1")
                SwitchControls(true, false);
            else
                SwitchControls(false, true);
        }

        private void SwitchControls(bool isLabelsShow, bool isControlsShow)
        {
            // labels
            this.lblCaseYear.Visible = isLabelsShow;
            this.lblName.Visible = isLabelsShow;
            this.lblCase1.Visible = isLabelsShow;
            this.lblCase2.Visible = isLabelsShow;
            this.lblCase3.Visible = isLabelsShow;
            this.lblCase4.Visible = isLabelsShow;
            this.lblCase5.Visible = isLabelsShow;
            this.lblCase6.Visible = isLabelsShow;
            this.lblCase7.Visible = isLabelsShow;
            this.lblCase8.Visible = isLabelsShow;
            this.lblCase9.Visible = isLabelsShow;
            this.lblCase10.Visible = isLabelsShow;
            this.lblCase11.Visible = isLabelsShow;
            this.lblCase12.Visible = isLabelsShow;
            this.lblCase13.Visible = isLabelsShow;
            this.lblCase14.Visible = isLabelsShow;
            this.lblCase15.Visible = isLabelsShow;
            this.lblCase16.Visible = isLabelsShow;
            this.lblCase17.Visible = isLabelsShow;
            this.lblCase18.Visible = isLabelsShow;
            this.lblCase19.Visible = isLabelsShow;
            this.lblCase20.Visible = isLabelsShow;
            this.lblCase21.Visible = isLabelsShow;
            this.lblCase22.Visible = isLabelsShow;
            this.lblCase23.Visible = isLabelsShow;
            this.lblCase24.Visible = isLabelsShow;
            this.lblCase25.Visible = isLabelsShow;
            this.lblCase26.Visible = isLabelsShow;
            this.lblCase27.Visible = isLabelsShow;
            this.lblCase28.Visible = isLabelsShow;

            this.lblOthers.Visible = isLabelsShow;
            this.lblFiller.Visible = isLabelsShow;
            this.lblIncharger.Visible = isLabelsShow;
            this.lblFillDate.Visible = isLabelsShow;

            // controls
            this.txtName.Visible = isControlsShow;
            this.txtCase1.Visible = isControlsShow;
            this.txtCase2.Visible = isControlsShow;
            this.txtCase3.Visible = isControlsShow;
            this.txtCase4.Visible = isControlsShow;
            this.txtCase5.Visible = isControlsShow;
            this.txtCase6.Visible = isControlsShow;
            this.txtCase7.Visible = isControlsShow;
            this.txtCase8.Visible = isControlsShow;
            this.txtCase9.Visible = isControlsShow;
            this.txtCase10.Visible = isControlsShow;
            this.txtCase11.Visible = isControlsShow;
            this.txtCase12.Visible = isControlsShow;
            this.txtCase13.Visible = isControlsShow;
            this.txtCase14.Visible = isControlsShow;
            this.txtCase15.Visible = isControlsShow;
            this.txtCase16.Visible = isControlsShow;
            this.txtCase17.Visible = isControlsShow;
            this.txtCase18.Visible = isControlsShow;
            this.txtCase19.Visible = isControlsShow;
            this.txtCase20.Visible = isControlsShow;
            this.txtCase21.Visible = isControlsShow;
            this.txtCase22.Visible = isControlsShow;
            this.txtCase23.Visible = isControlsShow;
            this.txtCase24.Visible = isControlsShow;
            this.txtCase25.Visible = isControlsShow;
            this.txtCase26.Visible = isControlsShow;
            this.txtCase27.Visible = isControlsShow;
            this.txtCase28.Visible = isControlsShow;

            this.txtYear.Visible = isControlsShow;
            this.txtMonth.Visible = isControlsShow;
            this.lblOMonth.Visible = isControlsShow;
            this.lblOYear.Visible = isControlsShow;
            this.txtOthers.Visible = isControlsShow;
            this.txtFiller.Visible = isControlsShow;
            this.txtIncharger.Visible = isControlsShow;
            this.dtFillDate.Visible = isControlsShow;
            this.hr11.Visible = isControlsShow;
            this.TBSubmit.Visible = isControlsShow;
        }

        private void LoadForView(PerformanceCaseDataSet.T_PerformanceCaseDataTable loadDT)
        {
            var loadRow = loadDT[0];

            this.lblCaseYear.Text = loadRow.FillYear + "年" + loadRow.FillMonth + "月";
            this.lblName.Text = loadRow.Name;
            this.lblCase1.Text = loadRow.Case_1;
            this.lblCase2.Text = loadRow.Case_2;
            this.lblCase3.Text = loadRow.Case_3;
            this.lblCase4.Text = loadRow.Case_4;
            this.lblCase5.Text = loadRow.Case_5;
            this.lblCase6.Text = loadRow.Case_6;
            this.lblCase7.Text = loadRow.Case_7;
            this.lblCase8.Text = loadRow.Case_8;
            this.lblCase9.Text = loadRow.Case_9;
            this.lblCase10.Text = loadRow.Case_10;
            this.lblCase11.Text = loadRow.Case_11;
            this.lblCase12.Text = loadRow.Case_12;
            this.lblCase13.Text = loadRow.Case_13;
            this.lblCase14.Text = loadRow.Case_14;
            this.lblCase15.Text = loadRow.Case_15;
            this.lblCase16.Text = loadRow.Case_16;
            this.lblCase17.Text = loadRow.Case_17;
            this.lblCase18.Text = loadRow.Case_18;
            this.lblCase19.Text = loadRow.Case_19;
            this.lblCase20.Text = loadRow.Case_20;
            this.lblCase21.Text = loadRow.Case_21;
            this.lblCase22.Text = loadRow.Case_22;
            this.lblCase23.Text = loadRow.Case_23;
            this.lblCase24.Text = loadRow.Case_24;
            this.lblCase25.Text = loadRow.Case_25;
            this.lblCase26.Text = loadRow.Case_26;
            this.lblCase27.Text = loadRow.Case_27;
            this.lblCase28.Text = loadRow.Case_28;

            this.lblOthers.Text = loadRow.Others;
            this.lblFiller.Text = loadRow.Filler;
            this.lblIncharger.Text = loadRow.Incharger;
            this.lblFillDate.Text = loadRow.FillDate.ToString(JCYConstant.Date_Format_CN);

        }

        private void LoadForEdit(PerformanceCaseDataSet.T_PerformanceCaseDataTable loadDT)
        {
            var loadRow = loadDT[0];

            this.txtYear.Text = loadRow.FillYear;
            this.txtMonth.Text = loadRow.FillMonth;
            this.txtName.Text = loadRow.Name;
            this.txtCase1.Text = loadRow.Case_1;
            this.txtCase2.Text = loadRow.Case_2;
            this.txtCase3.Text = loadRow.Case_3;
            this.txtCase4.Text = loadRow.Case_4;
            this.txtCase5.Text = loadRow.Case_5;
            this.txtCase6.Text = loadRow.Case_6;
            this.txtCase7.Text = loadRow.Case_7;
            this.txtCase8.Text = loadRow.Case_8;
            this.txtCase9.Text = loadRow.Case_9;
            this.txtCase10.Text = loadRow.Case_10;
            this.txtCase11.Text = loadRow.Case_11;
            this.txtCase12.Text = loadRow.Case_12;
            this.txtCase13.Text = loadRow.Case_13;
            this.txtCase14.Text = loadRow.Case_14;
            this.txtCase15.Text = loadRow.Case_15;
            this.txtCase16.Text = loadRow.Case_16;
            this.txtCase17.Text = loadRow.Case_17;
            this.txtCase18.Text = loadRow.Case_18;
            this.txtCase19.Text = loadRow.Case_19;
            this.txtCase20.Text = loadRow.Case_20;
            this.txtCase21.Text = loadRow.Case_21;
            this.txtCase22.Text = loadRow.Case_22;
            this.txtCase23.Text = loadRow.Case_23;
            this.txtCase24.Text = loadRow.Case_24;
            this.txtCase25.Text = loadRow.Case_25;
            this.txtCase26.Text = loadRow.Case_26;
            this.txtCase27.Text = loadRow.Case_27;
            this.txtCase28.Text = loadRow.Case_28;

            this.txtOthers.Text = loadRow.Others;
            this.txtFiller.Text = loadRow.Filler;
            this.txtIncharger.Text = loadRow.Incharger;
            this.hfFillDate.Value = loadRow.FillDate.ToString();

        }



        protected void imgSubmit_Click(object sender, ImageClickEventArgs e)
        {
            using (PerformanceCaseProxy proxy = new PerformanceCaseProxy())
            {
                if (Session["EditPerformanceCaseDT"] != null)
                {
                    var editDT = (PerformanceCaseDataSet.T_PerformanceCaseDataTable)Session["EditPerformanceCaseDT"];
                    var editRow = editDT[0];

                    editRow.Name = this.txtName.Text;
                    editRow.FillYear = this.txtYear.Text;
                    editRow.FillMonth = this.txtMonth.Text;

                    editRow.Case_1 = this.txtCase1.Text;
                    editRow.Case_2 = this.txtCase2.Text;
                    editRow.Case_3 = this.txtCase3.Text;
                    editRow.Case_4 = this.txtCase4.Text;
                    editRow.Case_5 = this.txtCase5.Text;
                    editRow.Case_6 = this.txtCase6.Text;
                    editRow.Case_7 = this.txtCase7.Text;
                    editRow.Case_8 = this.txtCase8.Text;
                    editRow.Case_9 = this.txtCase9.Text;
                    editRow.Case_10 = this.txtCase10.Text;
                    editRow.Case_11 = this.txtCase11.Text;
                    editRow.Case_12 = this.txtCase12.Text;
                    editRow.Case_13 = this.txtCase13.Text;
                    editRow.Case_14 = this.txtCase14.Text;
                    editRow.Case_15 = this.txtCase15.Text;
                    editRow.Case_16 = this.txtCase16.Text;
                    editRow.Case_17 = this.txtCase17.Text;
                    editRow.Case_18 = this.txtCase18.Text;
                    editRow.Case_19 = this.txtCase19.Text;
                    editRow.Case_20 = this.txtCase20.Text;
                    editRow.Case_21 = this.txtCase21.Text;
                    editRow.Case_22 = this.txtCase22.Text;
                    editRow.Case_23 = this.txtCase23.Text;
                    editRow.Case_24 = this.txtCase24.Text;
                    editRow.Case_25 = this.txtCase25.Text;
                    editRow.Case_26 = this.txtCase26.Text;
                    editRow.Case_27 = this.txtCase27.Text;
                    editRow.Case_28 = this.txtCase28.Text;

                    editRow.Others = this.txtOthers.Text;
                    editRow.Filler = this.txtFiller.Text;
                    editRow.FillDate = this.dtFillDate.SelectedDate;
                    editRow.Incharger = this.txtIncharger.Text;

                    JCYUtility.UpdateCommonFields(editRow, "Iori");
                    proxy.UpdatePerformanceCase(editDT);

                    Response.Redirect("PerformanceCase.aspx");
                }


            }
        }

        protected void imgCancel_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("PerformanceCase.aspx");
        }

        protected void lkbEditPerformanceCase_Click(object sender, EventArgs e)
        {
            this.hfIsEdit.Value = "1";
            SwitchControls(false, true);
            this.dtFillDate.SelectedDate = Convert.ToDateTime(this.hfFillDate.Value);
        }
    }
}
